package kr.hkit.powerjava.chapter25;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import org.omg.CORBA.portable.UnknownException;

public class QuizClient {
	public static void main(String[] args)throws IOException {
		Socket quizSocket = null;
		PrintWriter out = null;
		BufferedReader in = null;
		BufferedReader user = null;
		
		String serverMsg;
		String sendMsg;
		try{
		quizSocket = new Socket("localhost", 5555);
		out = new PrintWriter(quizSocket.getOutputStream(),true);
		in = new BufferedReader(new InputStreamReader(quizSocket.getInputStream()));
		}catch(UnknownException e){
			System.out.println(" localHost에 접근할 수 없습니다.");
			System.exit(-1);
			
		}catch(IOException e){
			e.printStackTrace();
			
		}
		
		while ( (serverMsg = in.readLine()) != null) {
			System.out.println("서버 : " +  serverMsg);//서버로부터 온 메시지 출력
			if (serverMsg.equalsIgnoreCase("quit")){
				break;
			}
			sendMsg = user.readLine();
			if (sendMsg != null){
				System.out.println("클라이언트 :" + sendMsg);
				out.println(sendMsg);
			}
		}
		user.close();
		in.close();
		out.close();
		quizSocket.close();
	}
}
